package cn.iune.erp.accounting;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;


@Transactional
@Service
public class AccountVoucherDetailServiceImpl implements IAccountVoucherDetailService {
	
	@Autowired
	private IAccountVoucherDetailDao dao;
	
	@Override
	public void insert(AccountVoucherDetail obj) {
		dao.insert(obj);
	}

	@Transactional
	@Override
	public void delete(String fID) {
		dao.delete(fID);
	}

	@Transactional
	@Override
	public void update(AccountVoucherDetail obj) {
		dao.update(obj);
	}

	@Transactional
	@Override
	public int save(JSONObject object) throws Throwable {
		// TODO Auto-generated method stub
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
			AccountVoucherDetail obj = JSON.toJavaObject(javaObject, AccountVoucherDetail.class);
//			System.out.println(JSON.toJSONString(javaObject));
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				dao.update(obj);
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
			}
		}
		return 0;
	}

	
	@Override
	public List<AccountVoucherDetail> getByMasterID(String fMasterID) {
		return dao.getByMasterID(fMasterID);
	}

	@Transactional
	@Override
	public int deleteByMasterID(String fMasterID){
		List<AccountVoucherDetail> as = dao.getByMasterID(fMasterID);
		for(AccountVoucherDetail a:as){
			dao.delete(a.getfID());
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int confirm(String fID) {
		AccountVoucherDetail a = dao.getByID(fID);
		return this.confirm(a);
	}
	
	@Transactional
	@Override
	public int active(String fID) {
		AccountVoucherDetail a = dao.getByID(fID);
		return this.active(a);
	}
	
	@Transactional
	@Override
	public int confirm(AccountVoucherDetail a) {
		a.setState("confirm");
		dao.update(a);
		return 0;
	}
	
	@Transactional
	@Override
	public int active(AccountVoucherDetail a) {
		a.setState("active");
		dao.update(a);
		return 0;
	}

	@Transactional
	@Override
	public int confirmByMasterID(String fMasterID){
		List<AccountVoucherDetail> as = dao.getByMasterID(fMasterID);
		for(AccountVoucherDetail a:as){
			this.confirm(a);
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int activeByMasterID(String fMasterID){
		List<AccountVoucherDetail> as = dao.getByMasterID(fMasterID);
		for(AccountVoucherDetail a:as){
			this.active(a);
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int open(String fID) {
		AccountVoucherDetail a = dao.getByID(fID);
		a.setStatus("open");
		dao.update(a);
		return 0;
	}
	
	@Transactional
	@Override
	public int close(String fID) {
		AccountVoucherDetail a = dao.getByID(fID);
		a.setStatus("close");
		dao.update(a);
		return 0;
	}
	
	@Transactional
	@Override
	public int open(AccountVoucherDetail a) {
		a.setStatus("open");
		dao.update(a);
		return 0;
	}
	
	@Transactional
	@Override
	public int close(AccountVoucherDetail a) {
		a.setStatus("close");
		dao.update(a);
		return 0;
	}

	@Transactional
	@Override
	public int openByMasterID(String fMasterID){
		List<AccountVoucherDetail> as = dao.getByMasterID(fMasterID);
		for(AccountVoucherDetail a:as){
			this.open(a);
		}
		return 0;
	}
	@Transactional
	@Override
	public int closeByMasterID(String fMasterID){
		List<AccountVoucherDetail> as = dao.getByMasterID(fMasterID);
		for(AccountVoucherDetail a:as){
			this.close(a);
		}
		return 0;
	}
	
	//审核通过，改变审核状态为confirm
	@Transactional
	@Override
	public int agreeIt(String fID) {
		//更新数据库工具入库单的单据状态（同时更新单据的结存数量、金额、件数）
		AccountVoucherDetail a = dao.getByID(fID);
		return this.agreeIt(a);
	}
	//审核拒绝，改变审核状态为active
	@Transactional
	@Override
	public int unAgree(String fID) {
		AccountVoucherDetail a = dao.getByID(fID);
		//更新数据库工具入库单的单据状态
		return this.unAgree(a);
	}	

	//审核通过，改变审核状态为confirm
	@Transactional
	@Override
	public int agreeIt(AccountVoucherDetail a) {
		//更新数据库工具入库单的单据状态（同时更新单据的结存数量、金额、件数）
		
		a.setAudit("confirm");
		a.setConfirmDate(Utils.getCurrentDate());
		
		dao.update(a);
		
		return 0;
	}
	
	//审核拒绝，改变审核状态为active
	@Transactional
	@Override
	public int unAgree(AccountVoucherDetail a) {

		//更新数据库工具入库单的单据状态

		a.setAudit("active");
		a.setConfirmDate(null);
		
		dao.update(a);

		return 0;
	}	


	@Transactional
	@Override
	public int agreeItByMasterID(String fMasterID){
		List<AccountVoucherDetail> as = dao.getByMasterID(fMasterID);
		for(AccountVoucherDetail a : as){
			this.agreeIt(a);
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int unAgreeByMasterID(String fMasterID){
		List<AccountVoucherDetail> as = dao.getByMasterID(fMasterID);
		for(AccountVoucherDetail a : as){
			this.unAgree(a);
		}
		return 0;
	}
	
	
	@Override
	public AccountVoucherDetail packing(AccountVoucher accountVoucher){
		
		AccountVoucherDetail obj = new AccountVoucherDetail();
		String fID = Utils.getUUID();
		obj.setfID(fID);
		
		obj.setBillType(accountVoucher.getBillType());
		obj.setBillTypeName(accountVoucher.getBillTypeName());
		obj.setSystemCode(accountVoucher.getSystemCode());
		obj.setDirection(accountVoucher.getDirection());
		
		obj.setState(accountVoucher.getState());
		obj.setStatus(accountVoucher.getStatus());
		obj.setAudit(accountVoucher.getAudit());
		obj.setOrderDate(accountVoucher.getOrderDate());
		
		obj.setfMasterID(accountVoucher.getfID());
		obj.setVoucherNo(accountVoucher.getVoucherNo());
		
		obj.setDebitAmount(BigDecimal.ZERO);
		obj.setCreditAmount(BigDecimal.ZERO);
		
		//分录底稿
		obj.setEntryAmount(BigDecimal.ZERO);
		obj.setDob(0);
		
		//子表明细号
		int maxDetailNo = accountVoucher.getMaxDetailNo() + 1;
		String serialNo = String.format("%05d", maxDetailNo);
		obj.setSerialNo(serialNo);
		
		obj.setUserId(accountVoucher.getUserId());
		obj.setEnterpriseNo(accountVoucher.getEnterpriseNo());
		obj.setEnterpriseName(accountVoucher.getEnterpriseName());
		
		obj.setVersion(0);
		
		return obj;
	}

	

}
